import prisma from '../prisma/index.js'

export const getCommentsService = async (content, skip, pagesize) => {
  try {
    return await prisma.comment.findMany({
      where: {
        content: {
          contains: content,
        },
      },
      include: {
        post: {
          select: {
            title: true,
          },
        },
      },
      skip,
      take: pagesize,
    })
  } catch (err) {
    throw err
  }
}

export const getCommentService = async id => {
  try {
    return await prisma.comment.findUnique({
      where: { id },
      include: {
        post: {
          select: {
            title: true,
          },
        },
      },
    })
  } catch (err) {
    throw err
  }
}

export const postCommentService = async data => {
  try {
    return await prisma.comment.create({
      data,
    })
  } catch (err) {
    throw err
  }
}

export const patchCommentService = async (id, data) => {
  try {
    return await prisma.comment.update({
      where: { id },
      data,
    })
  } catch (err) {
    throw err
  }
}

export const deleteCommentService = async id => {
  try {
    return await prisma.comment.deleteMany({
      where: { id },
    })
  } catch (err) {
    throw err
  }
}


